package com.egao.common.manage.student;

import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.module.student.constants.StudentConstants;
import com.egao.common.module.student.entity.Student;
import com.egao.common.module.student.entity.vo.StudentFormVO;
import com.egao.common.module.student.service.StudentService;
import com.egao.common.module.system.entity.Dict;
import com.egao.common.module.system.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 学生生源管理
 * @author lym
 */
@Api(tags = "学生生源管理")
@RestController
@RequestMapping("${api}")
public class StudentStudentController extends BaseController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private DictService dictService;

    private final String title = "学生生源管理";

    @PreAuthorize("hasAuthority('student')")
    @OperLog(value = title, desc = "学生详情信息")
    @ApiOperation(value = "学生详情信息", notes = "")
    @GetMapping("/student/student/detail")
    public JsonResult detail() {
        Student student = studentService.getByUserId(getLoginUserId());

        if (student == null) {
            return JsonResult.error("没有找到学生数据");
        }

        if (StudentConstants.STUDENT_QUESTIONNAIRE_WAIT.equals(student.getQuestionnaireState())) {
            return JsonResult.error(302, "请先完成就业意向调查");
        }

        StudentFormVO studentFormVO = studentService.getStudentFormVO(student.getId());
        Map<String, List<Dict>> dictMap = dictService.studentForm();
        return JsonResult.ok().setData(studentFormVO).put("dictMap", dictMap);
    }

    @PreAuthorize("hasAuthority('student')")
    @OperLog(value = title, desc = "提交生源信息", result = true)
    @ApiOperation(value = "提交生源信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentFormVO", value = "StudentFormVO", required = true, dataType = "StudentFormVO")
    })
    @PutMapping("/student/student/update")
    public JsonResult update(@RequestBody StudentFormVO studentFormVO) {
        return studentService.studentSubmit(studentFormVO, getLoginUserId());
    }

}
